<template>
  <div class="userLoginView">
    <h2 style="margin-bottom: 16px">⽤户注册</h2>
    <a-form
      style="max-width: 480px; margin: 0 auto"
      label-align="left"
      auto-label-width
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item
        :rules="[
          { required: true, message: '账号不能为空' },
          { minLength: 4, message: '账号⻓度不能低于4位' },
        ]"
        field="userAccount"
        label="账号"
      >
        <a-input v-model="form.userAccount" placeholder="请输⼊账号" />
      </a-form-item>
      <a-form-item
        :rules="[{ required: true, message: '⽤户名不能为空' }]"
        field="userName"
        label="⽤户名"
      >
        <a-input v-model="form.userName" placeholder="请输⼊⽤户名" />
      </a-form-item>
      <a-form-item
        :rules="[
          { required: true, message: '密码不能为空' },
          { minLength: 8, message: '密码⻓度不能低于8位' },
        ]"
        :validate-trigger="['change', 'input']"
        field="userPassword"
        tooltip="密码不少于8位"
        label="密码"
      >
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输⼊密码"
        />
      </a-form-item>
      <a-form-item
        :rules="[
          { required: true, message: '密码不能为空' },
          { minLength: 8, message: '密码⻓度不能低于8位' },
        ]"
        :validate-trigger="['change', 'input']"
        field="checkPassword"
        tooltip="密码不少于8位"
        label="重复密码"
      >
        <a-input-password
          v-model="form.checkPassword"
          placeholder="请重复输⼊密码"
        />
      </a-form-item>
      <a-space wrap>
        <a-button
          size="large"
          shape="round"
          type="secondary"
          status="success"
          @click="toIndex"
        >
          首 页
        </a-button>
        <a-button
          style="margin: 25px; width: 150px"
          size="large"
          shape="round"
          type="primary"
          @click="handleSubmit"
        >
          注 册
        </a-button>
      </a-space>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserRegisterRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
  userName: "",
} as UserRegisterRequest);
const router = useRouter();
const store = useStore();
const handleSubmit = async () => {
  // 提前判断密码和重复密码是否相同
  if (
    form.userPassword?.length !== form.checkPassword?.length ||
    form.userPassword !== form.checkPassword
  ) {
    message.error("两次输⼊的密码不⼀致");
    return;
  }
  // 使用OpenAPI根据接口文档生成的代码，调用后端接口，将表单数据传递给后端
  const res = await UserControllerService.userRegisterUsingPost(form);
  if (res.code === 0) {
    // 注册成功 ,跳转到登录页面
    console.log(res);
    // 获取登录用户信息存到全局状态管理,因为user.ts中获取登录用户的方法使用的async ，所以使用await使异步变同步
    await store.dispatch("user/getLoginUser");
    message.success("注册成功啦");
    // 路由到主页
    router.push({
      path: "/user/login",
      replace: true,
    });
  } else {
    message.error("注册失败，" + res.code);
  }
};
const toIndex = () => {
  router.push({
    path: `/`,
  });
};
</script>
